package QianFeng16.JSON.Create;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

public class CreateJSON {

    //生成Json
    @Test
    public void createJSON() {
        List<Name> list = new ArrayList<>();
        list.add(new Name("vince", "ma", "123456@qq.com"));
        list.add(new Name("jack", "wang", "jack@qq.com"));
        JsonArray array = new JsonArray();//创建JSON数组
        for (Name name : list) {
            JsonObject object = new JsonObject();//创建JSON对象
            object.addProperty("firstName", name.getFirstName());
            object.addProperty("lastName", name.getLastName());
            object.addProperty("email", name.getEmail());
            //将对象添加到数组中
            array.add(object);
        }
        System.out.println(array.toString());
    }
}
